package com.yao.club.service.impl;

import com.yao.club.entity.UserEntity;
import com.yao.club.entity.dto.SysAddressCity;
import com.yao.club.entity.dto.SysAddressDistrict;
import com.yao.club.entity.dto.SysAddressProvince;
import com.yao.club.entity.dto.UserAddress;
import com.yao.club.entity.request.AddUserAddressRequestEntity;
import com.yao.club.entity.request.UpdateUserAddressRequestEntity;
import com.yao.club.entity.result.ResponseEntity;
import com.yao.club.mapper.UserMapper;
import com.yao.club.reposittory.SysAddressCityRepository;
import com.yao.club.reposittory.SysAddressDistrictRepository;
import com.yao.club.reposittory.SysAddressProvinceRepository;
import com.yao.club.reposittory.UserAddressRepository;
import com.yao.club.service.UserAddressService;
import com.yao.club.utils.ResponseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserAddressServiceImpl implements UserAddressService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private UserAddressRepository userAddressRepository;

    @Autowired
    private SysAddressProvinceRepository provinceRepository;

    @Autowired
    private SysAddressCityRepository cityRepository;
    @Autowired
    private SysAddressDistrictRepository districtRepository;

    @Override
    public ResponseEntity<List<UserAddress>> getUserAddresses(String username) {


        UserEntity user = userMapper.select(username);
        if (user == null) {
            return ResponseUtils.failed("用户不存在");
        }

        List<UserAddress> addressList = userAddressRepository.findByUserId(user.member_id);
        return ResponseUtils.success(addressList);
    }

    @Override
    public ResponseEntity<List<SysAddressProvince>> getAllProvinces() {
        List<SysAddressProvince> provinces = provinceRepository.findAll();
        return ResponseUtils.success(provinces);
    }


    @Override
    public ResponseEntity<List<SysAddressCity>> getCitiesByProvinceId(String provinceId) {
        List<SysAddressCity> cities = cityRepository.findByParentCode(provinceId);
        return ResponseUtils.success(cities);
    }


    @Override
    public ResponseEntity<List<SysAddressDistrict>> getDistrictsByCityId(String cityId) {
        List<SysAddressDistrict> districts = districtRepository.findByCityCode_CityCode(cityId);
        return ResponseUtils.success(districts);
    }

    @Override
    public ResponseEntity<Boolean> addUserAddress(AddUserAddressRequestEntity request, String username) {
        UserEntity user = userMapper.select(username);
        if (user == null) {
            return ResponseUtils.failed("用户不存在");
        }

        UserAddress newAddress = new UserAddress();
        newAddress.setAddress(request.getAddress());
        newAddress.setCityCode(request.getCityCode());
        newAddress.setCityName(request.getCityName());
        newAddress.setDistrictCode(request.getDistrictCode());
        newAddress.setDistrictName(request.getDistrictName());
        newAddress.setIsDefault(request.isDefault() ? 1:0);
        newAddress.setPostalCode(request.getPostalCode());
        newAddress.setProvinceCode(request.getProvinceCode());
        newAddress.setProvinceName(request.getProvinceName());
        newAddress.setReceiverName(request.getReceiverName());
        newAddress.setReceiverPhone(request.getReceiverPhone());
        newAddress.setUserId(user.getMember_id());

        userAddressRepository.save(newAddress); // Save the new address
        return ResponseUtils.success(true);
    }

    @Override
    public ResponseEntity<Boolean> updateUserAddress(UpdateUserAddressRequestEntity request, String username) {
        UserEntity user = userMapper.select(username);
        if (user == null) {
            return ResponseUtils.failed("用户不存在");
        }

        UserAddress existingAddress = userAddressRepository.findById(request.getId()).orElse(null);
        if (existingAddress == null || !existingAddress.getUserId().equals(user.getMember_id())) {
            return ResponseUtils.failed("地址不存在或用户不匹配");
        }

        // Update the existing address with new values
        existingAddress.setAddress(request.getAddress());
        existingAddress.setCityCode(request.getCityCode());
        existingAddress.setCityName(request.getCityName());
        existingAddress.setDistrictCode(request.getDistrictCode());
        existingAddress.setDistrictName(request.getDistrictName());
        existingAddress.setIsDefault(request.isDefault() ? 1:0);
        existingAddress.setPostalCode(request.getPostalCode());
        existingAddress.setProvinceCode(request.getProvinceCode());
        existingAddress.setProvinceName(request.getProvinceName());
        existingAddress.setReceiverName(request.getReceiverName());
        existingAddress.setReceiverPhone(request.getReceiverPhone());

        userAddressRepository.save(existingAddress); // Save the updated address
        return ResponseUtils.success(true);
    }
    @Override
    public ResponseEntity<Boolean> deleteUserAddress(Long addressId, String username) {
        UserEntity user = userMapper.select(username);
        if (user == null) {
            return ResponseUtils.failed("用户不存在");
        }

        UserAddress existingAddress = userAddressRepository.findById(addressId).orElse(null);
        if (existingAddress == null || !existingAddress.getUserId().equals(user.getMember_id())) {
            return ResponseUtils.failed("地址不存在或用户不匹配");
        }

        userAddressRepository.delete(existingAddress); // Delete the address
        return ResponseUtils.success(true);
    }
}
